﻿using System.IO;
using Avalonia.Controls;
using GitNet.WindowsApp.AddViews;
using GitNet.WindowsApp.Models;
using GitNet.WindowsApp.Views;
using LibGit2Sharp;

namespace GitNet.WindowsApp.ViewModels;

public class MainWindowViewModel : ViewModelBase
{
    
    #region Pror

    private string? _handoffText;

    public string? HandoffText
    {
        get => _handoffText;
        set
        {
            if(value == _handoffText || string.IsNullOrEmpty(value))return;
            if (_handoffText == "切换到文件夹视图" && Content is RepositoryView repository)
            {
                var path = (repository.DataContext as RepositoryViewModel)?.RepositoryPath;
                if (string.IsNullOrEmpty(path)) return;
                Content = new FolderView() { DataContext = new FolderViewModel(path) };
            }

            if (_handoffText == "切换到仓库视图" && Content is FolderView folder)
            {
                var path = (folder.DataContext as FolderViewModel)?.FolderPath;
                if (string.IsNullOrEmpty(path)) return;
                ProjectData.Save(new PathData(path));
                Content = new RepositoryView() { DataContext = new RepositoryViewModel(path) };
            }
            SetField(ref _handoffText, value);
        }
    }

    private Control? _content = new StartView() { DataContext = new StartViewModel() };

    public Control? Content
    {
        get => _content;
        set => SetField(ref _content, value);
    }

    #endregion

    #region Funtion

    public async void Open()
    {
        var folder = new OpenFolderDialog();
        var result = await folder.ShowAsync(new Window());
        if (!Directory.Exists(result)) return;
        ToView(result);
    }

    public void Clone(string url, string path)
    {
        string folderName;
        var a = url.Split("/")[^1];
        if (a[^4..] != ".git")
        {
            url += ".git";
            folderName = a[..];
        }
        else
        {
            folderName = a[..^4];
        }
        var d = new DirectoryInfo(path);
        if (d.GetFileSystemInfos().Length > 0)
            folderName = "";
        if (!string.IsNullOrEmpty(folderName))
            path = Path.Combine(path,folderName);
        
        Repository.Clone(url, path);
        ToView(path);
    }

    public void Clone(string url, string path, string userName, string password)
    {
        var co = new CloneOptions
        {
            CredentialsProvider = (_url, _user, _cred)
                => new UsernamePasswordCredentials { Username = userName, Password = password }
        };
        Repository.Clone(url, path, co);
        ToView(path);
    }

    #endregion

    #region View

    public void ToView(string path)
    {
        if(!Directory.Exists(path))
        {
            ProjectData.Remove(new PathData(path));
            if (Content is not StartView view) return;
            if (view.DataContext is StartViewModel model)
                model.Paths.RemoveAt(model.Inquire(path));
            
            return;
        }
        ProjectData.Save(new PathData(path));
        Content = new RepositoryView() { DataContext = new RepositoryViewModel(path) };
        HandoffText = "切换到文件夹视图";
    }
    
    public void ToHome()
    {
        Content = new StartView() { DataContext = new StartViewModel() };
        HandoffText = "";
    }

    public void ToFolderView(string result)
    {
        Content = new FolderView() { DataContext = new FolderViewModel(result) };
    }

    public void ToCreateView()
    {
        Content = new CreateFolderView();
    }

    public void ToCloneView()
    {
        Content = new CloneView();
    }

    public void Handoff()
    {
        switch (HandoffText)
        {
            case "切换到仓库视图":
                HandoffText = "切换到文件夹视图";
                return;
            case "切换到文件夹视图":
                HandoffText = "切换到仓库视图";
                break;
        }
    }
    
    #endregion
    
}